//////////////////////////////////////////////////////////////////////////////////
//	Controller.cs
//	
//	Written by Cosic Nadin and Franz Thomas Reinisch FH-Joanneum ITM06
//  
//////////////////////////////////////////////////////////////////////////////////



using System;
using System.Collections.Generic;
using System.Text;
using WiimoteLib;
using System.Windows.Forms;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Configuration;
using System.Timers;

namespace WiiFlipChart
{
    public class Controller  
    {


      

        private Point CurrentDrawCursorPoint;
        private Boolean CursorInDrawField = false;

        private int BoardWindowSizeX;
        private int BoardWindowSizeY;
        
        private int DrawFieldSizeX = 0;
        private int DrawFieldSizeY = 0;

        private int DrawFieldOffsetX = 2;
        private int DrawFieldOffsetY = 20;

        private int PointOffsetX = 0;
        private int PointOffsetY = 0;
        
        private int BoardWindowLocationX = 0;
        private int BoardWindowLocationY = 0;

        private int x, y;
        private float fx, fy;
        private float fxlast, fylast;
        private const float fmid = 0.5F; //Mid Level of IR Value

        private int DrawFieldPositionX = 50;
        private int DrawFieldPositionY = 80;

        
    
       


        public event EventHandler ButtonAChanged;
        public event EventHandler CurrentDrawCursorPointChanged;


       
        /// <summary>
        /// Fire the event if ther button "A" is pressed and cursor is in drawfield
        /// </summary>
        /// <param name="ea"></param>
        protected void OnButtonAChanged(EventArgs ea)
        {
            if (ButtonAChanged != null)
            {
                ButtonAChanged(this, ea);
            }
        }


        /// <summary>
        /// Fire the event if the current drawcursor position was changed
        /// </summary>
        /// <param name="ea"></param>
        protected void OnCurrentDrawCursorPointChanged(EventArgs ea)
        {
            if (CurrentDrawCursorPointChanged != null)
            {
                CurrentDrawCursorPointChanged(this, ea);
            }
        }



        static readonly Controller instance = new Controller();
    
        public static Controller Instance
        {
            get
            {
                return instance;
            }
        }

    
        Controller()
        {
            
            
            AppSettingsReader config = new AppSettingsReader();
                        
            BoardWindowSizeX = (int)config.GetValue("BoardWindowSizeX", typeof(int));
            BoardWindowSizeY = (int)config.GetValue("BoardWindowSizeY", typeof(int));
                               
            DrawFieldSizeX = (int)config.GetValue("DrawFieldSizeX", typeof(int));
            DrawFieldSizeY = (int)config.GetValue("DrawFieldSizeY", typeof(int));

            PointOffsetX = 5; // To correct the Position of mousepointer, that the pointer is there where you draw
            PointOffsetY = 23;
            
            BoardWindowLocationX = 0;
            BoardWindowLocationY = 0;

            //Subscribe WiimoteChangedEventHandler
            WiiMoteDetection.Instance.WiimoteChanged += new WiimoteChangedEventHandler(WiimoteChangedEvent);
        
        }


        /// <summary>
        /// Initialisation of all Classes
        /// </summary>
        public void Init()
        {

          WiiMoteDetection wiimotedetec = WiiMoteDetection.Instance;
          PageControl  pagecontrol = PageControl.Instance;
          Drawing drawing = Drawing.Instance;
          GestureDetection gd = GestureDetection.Instance;
          StrokeWidthControl swc = StrokeWidthControl.Instance;
          ColorControl cc = ColorControl.Instance;

        }






        public int getPointOffsetX()
        {
            return PointOffsetX;
        
        }

        public int getPointOffsetY()
        {
            return PointOffsetY;

        }


        public int getBoardWindowSizeX()
        {
            return BoardWindowSizeX;
        }

        public int getBoardWindowSizeY()
        {
            return BoardWindowSizeY;
        }
  

        public int getDrawFieldSizeX()
        {
            return DrawFieldSizeX;
        }

        public int getDrawFieldSizeY()
        {
            return DrawFieldSizeY;

        }
        public int getDrawFieldPositionX()
        {
            return DrawFieldPositionX;
        }

        public int getDrawFieldPositionY()
        {
            return DrawFieldPositionY;
        }



        public Boolean getCursorInDrawField()
        {
            return CursorInDrawField;
        }

       



        protected void WiimoteChangedEvent(object sender,WiimoteChangedEventArgs args)
        {

            WiimoteState ws = args.WiimoteState;


            fx = ws.IRState.X1;
            fy = ws.IRState.Y1;


            if (fx < 0.0009F)
            {
                fx = 0.0009F; fxlast = fx;
            }



            if (fy < 0.0013029F)
            {
                fy = 0.0013029F; fylast = fy;
            }



            //If there is an owerflow
            if (fx == 0.9995115F)
            {
                fx = fxlast; fy = fylast;
            }

            fylast = fy;
            fxlast = fx; 




            
            
            //Calculate in real x and y cortinates
            if (fx > fmid)
            {
                fx = fx - ((fx - fmid) * 2);
            }

            else if (fx < fmid)
            {
                fx = fx + ((fmid - fx) * 2);
            }


        

            fx = fx * (float)DrawFieldSizeX; //Multiplicator for draw Area  
            fy = fy * (float)DrawFieldSizeY;

            x = (int)fx;
            y = (int)fy;


            //if the cursor is in the drawfieldrange
            if (x < (DrawFieldSizeX - 3) && x > 5 && y < (DrawFieldSizeY -10 ) && y > 2)
            {

                CursorInDrawField = true;

                //Calculate Cursorposition and position the cursor
                x = x + DrawBoard.Instance.Location.X + DrawFieldPositionX + DrawFieldOffsetX;
                y = y + DrawBoard.Instance.Location.Y + DrawFieldPositionY + DrawFieldOffsetY;
                

                Cursor.Position = new Point(x, y);

                CurrentDrawCursorPoint = new Point(x - PointOffsetX, y - PointOffsetY);
                
                if (ws.ButtonState.A)
                {
                     //Fire the Event   
                    OnButtonAChanged(EventArgs.Empty);
                }

         

                //Fire the Event  If no drawing save the position of the current point to last point 
                OnCurrentDrawCursorPointChanged(EventArgs.Empty);

        
            }
            else
            { 
                CursorInDrawField = false; 
            }

        }

        /// <summary>
        /// give back the current pont of the drawcursor
        /// </summary>
        /// <returns> Point drawcursor</returns>
        public Point getCurrentDrawCursorPoint()
        {
            return CurrentDrawCursorPoint;
        }

    }
    
}